#!perl -T


use Test::More tests => 4;

use Data::PrintUtils qw(:ALL);
my @table = 
	(
	{COL1 => 1, Name => 'PID',  VALUE => '1a', XTRA1 => '111'},
	{COL1 => 1, Name => 'SID',  VALUE => 's1', XTRA1 => '112'},
	{COL1 => 1, Name => 'XV1',  VALUE => 'YY', XTRA1 => '116'},
	{COL1 => 1, Name => 'XV1',  VALUE => 'ZZ', XTRA1 => '116'},
	{COL1 => 1, Name => 'XV2',  VALUE => 'XX', XTRA1 => '117'},

	{COL1 => 2, Name => 'PID',  VALUE => '2a', XTRA1 => '221', XTRA2 => '1'},
	{COL1 => 2, Name => 'SID',  VALUE => 's2', XTRA1 => '221', XTRA2 => '2'},
	{COL1 => 2, Name => 'XV2',  VALUE => 'XX2', XTRA1 => '221', XTRA2 => '1'},
	);


my $concatRes = <<RES;
COL1  PID  SID     XV1  XV2.*
   1   1a   s1 YY \| ZZ  XX.*
   2   2a   s2    NULL  XX2.*
RES
my $replaceRes = <<RES;
COL1  PID  SID  XV1  XV2.*
   1   1a   s1   ZZ   XX.*
   2   2a   s2 NULL  XX2.*
RES
my $multiColReplaceRes = <<RES;
COL1  PID  SID  XV1  XV2 111 112       116 117     221.*
   1   1a   s1   ZZ   XX 111 112 116 | 116 117    NULL.*
   2   2a   s2 NULL  XX2 NULL NULL    NULL  NULL 221 | 221 | 221.*
RES
my $indenticalColReplaceRes = <<RES;
COL1  PID  SID  XV1  XV2 XTRA1 XTRA2.*
   1   1a   s1 YY \| ZZ   XX  NULL  NULL.*
   2   2a   s2    NULL  XX2   221  NULL.*
RES

my @newTable1 = pivotTable { ROWS => \@table, PIVOT_KEY => 'COL1', VALUE_HEADER_KEY=> 'Name', VALUE_KEY => 'VALUE'};
like (formatTable( { ROWS => \@newTable1, UNDEF_VALUE => 'NULL', COLUMNS => [qw/COL1 PID SID XV1 XV2/],}), qr/$replaceRes/, "Straight Pivot");
@newTable1 = pivotTable { ROWS => \@table, PIVOT_KEY => 'COL1', VALUE_HEADER_KEY=> 'Name', VALUE_KEY => 'VALUE', CONCAT_DUPLICATE => 1};
like (formatTable( { ROWS => \@newTable1, UNDEF_VALUE => 'NULL', COLUMNS => [qw/COL1 PID SID XV1 XV2/],}), qr/$concatRes/, "Concatanate Duplicates");
@newTable1 = pivotTable { ROWS => \@table, PIVOT_KEY => 'COL1', VALUE_HEADER_KEY=> [qw/Name XTRA1/], VALUE_KEY => [qw/VALUE XTRA1/], CONCAT_DUPLICATE => 1};
like (formatTable( { ROWS => \@newTable1, UNDEF_VALUE => 'NULL', COLUMNS => [qw/COL1 PID SID XV1 XV2 111 112 116 117 221/],}), qr/$multiColReplaceRes/, "Concatanate Duplicates - multiple keys");
@newTable1 = pivotTable { ROWS => \@table, PIVOT_KEY => 'COL1', VALUE_HEADER_KEY=> [qw/Name /], 
    VALUE_KEY => [qw/VALUE /], CONCAT_DUPLICATE => 1, INCLUDE_IDENTICAL => 1};
like (formatTable( { ROWS => \@newTable1, UNDEF_VALUE => 'NULL', COLUMNS => [qw/COL1 PID SID XV1 XV2 XTRA1 XTRA2/],}), qr/$indenticalColReplaceRes/, "Concatanate Duplicates - multiple keys");




